package com.ldk.api.handler;

import java.util.List;
import java.util.Map;

import com.ldk.base.framework.util.ConvertUtil;
import com.ldk.base.framework.util.RegexUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.ldk.api.Consts;
import com.ldk.base.framework.util.EncryptUtil;
import com.ldk.base.framework.util.PropertiesUtil;
import com.ldk.framework.entity.Dictdata;
import com.ldk.framework.entity.ServiceConfig;
import com.ldk.framework.service.IDictdataService;
import com.ldk.framework.service.IServiceConfigService;


/**
 * 请求处理基类
 * @author xiangwl
 *
 */
public abstract class BaseRequestHandler {

	/**
	 * 日志器
	 */
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());
	

	@Autowired
	private IDictdataService dictdataService;
	@Autowired
	private IServiceConfigService serviceConfigService;

	/**
	 * 检测是否包含必要参数
	 * @param parms 参数集
	 * @param keys 必须的参数Key
	 * @return true 所以参数Key都包括 false至少有一个参数Key不包含
	 */
	protected Boolean checkRequireParms(Map<String, Object> parms, String ...keys) {

		if(parms == null || parms.isEmpty()) return false;
		if(keys == null || parms.isEmpty()) return true;

		for(String key:keys){
			if(!parms.containsKey(key))
				return false;
		}

		return true;

	}
	
	protected String getImgPath()
	{
		try {
			String imgPath = PropertiesUtil.getProperty(PropertiesUtil.read(this.getClass().getResource("/").getPath() +"/config.properties"), Consts.IMAGE_PATH);
			if(!StringUtils.isEmpty(imgPath) && !imgPath.endsWith("/")){
				imgPath += "/";
			}
			return imgPath;
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return "";
	}

	protected String getImgRoot()
	{
		try {
			String imgRoot = PropertiesUtil.getProperty(PropertiesUtil.read(this.getClass().getResource("/").getPath() +"/config.properties"), Consts.IMAGE_ROOT);
			if(!StringUtils.isEmpty(imgRoot) && !imgRoot.endsWith("/")){
				imgRoot += "/";
			}
			return imgRoot;
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return "";
	}
	
	protected List<Dictdata>  getDictdataList(int dictId) {
		Dictdata query = new Dictdata();
		query.setEnabled(true);
		query.setDictId(dictId);
		
		return dictdataService.selectList("", query);
	}
	
	protected Dictdata getDictdataByName(int dictId, String dataName) {
		Dictdata query = new Dictdata();
		query.setEnabled(true);
		query.setDictId(dictId);
		query.setDataName(dataName);
		
		return dictdataService.selectOne("", query);
	}
	protected Dictdata getDictdataByValue(int dictId, String dataValue) {
		Dictdata query = new Dictdata();
		query.setEnabled(true);
		query.setDictId(dictId);
		query.setDataValue(dataValue);
		
		return dictdataService.selectOne("", query);
	}
	
	/**
	 * 获取服务业务代码
	 * @param serviceName
	 * @return
	 */
	protected String getServiceCode(String serviceName){
		ServiceConfig query = new ServiceConfig();
		query.setServiceName(serviceName);
		
		ServiceConfig model = serviceConfigService.selectOne("", query);
		if(model == null ) return "";
		return model.getServiceCode();
	}
	/**
	 * 支付密码加密
	 * @param payPwd
	 * @return
	 */
	protected String encodePayPwd(String payPwd) {
		
		return EncryptUtil.md5(payPwd+Consts.System_Sign_EncryptKey);
		
	}

	/**
	 * 预约时间最小间隔(分钟)
	 * @return
	 */
	protected int  getDepart_time() {
		Dictdata query = new Dictdata();
		query.setDataId(21);
		Dictdata entity = dictdataService.selectByKey("",query);
		int dataValue = 0;
		if(RegexUtil.isUInt(entity.getDataValue())) {
			dataValue = ConvertUtil.parseInt(ConvertUtil.parseString(entity.getDataValue()));
		}
		return dataValue;
	}
}
